<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="800" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode==='show'" ref="dialogForm" label-width="100px" label-position="right">
			<el-row>
				<el-col :span="12">
					<el-form-item label="标题" prop="title">
						<el-input v-model="form.title" placeholder="标题" clearable></el-input>
					</el-form-item>
					<el-form-item label="类型" prop="type">
						<el-radio-group v-model="form.type">
							<el-radio-button v-for="(item, index) in typeList" :key="index" :label="item.dictValue">{{item.dictItem}}</el-radio-button>
						</el-radio-group>
					</el-form-item>
					<el-form-item v-if="form.type==='menu'" label="组件" prop="component">
						<el-input v-model="form.component" placeholder="视图菜单需填写" clearable>
							<template #prepend>views/</template>
						</el-input>
					</el-form-item>
					<el-form-item v-if="form.type!=='button'" label="菜单图标" prop="icon">
						<sc-icon-select v-model="form.icon" clearable></sc-icon-select>
					</el-form-item>
					<el-form-item label="是否启用" prop="status">
						<el-switch v-model="form.status" active-value="1" inactive-value="0"></el-switch>
					</el-form-item>
					<el-form-item v-if="form.type!=='button'" label="隐藏菜单" prop="hidden">
						<el-switch v-model="form.hidden" :active-value="true" :inactive-value="false"></el-switch>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="上级菜单" prop="parentId">
						<el-cascader v-model="form.parentId" :options="options" :props="optionsProps" :show-all-levels="false" clearable style="width: 100%;"></el-cascader>
					</el-form-item>
					<el-form-item label="编码" prop="code">
						<el-input v-model="form.code" placeholder="组件名，按钮类型填写权限编码" clearable></el-input>
					</el-form-item>
					<el-form-item v-if="form.type==='menu'" label="路由地址" prop="route">
						<el-input v-model="form.route" placeholder="路由地址" clearable></el-input>
					</el-form-item>
					<el-form-item label="排序号" prop="orderNum">
						<el-input-number v-model="form.orderNum" :min="1"></el-input-number>
					</el-form-item>
					<el-form-item v-if="form.type!=='button'" label="固定标签" prop="affix">
						<el-switch v-model="form.affix" :active-value="true" :inactive-value="false"></el-switch>
					</el-form-item>
					<el-form-item label="备注" prop="remark">
						<el-input v-model="form.remark" clearable type="textarea"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!=='show'" type="primary" :loading="isSaving" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
import scIconSelect from '@/components/scIconSelect/index.vue'

export default {
	components: {
		scIconSelect
	},
	emits: ['success', 'closed'],
	data() {
		return {
			// 表单元素控制
			mode: "add",
			titleMap: {
				add: '新增',
				edit: '编辑',
				show: '查看'
			},
			visible: false,
			isSaving: false,
			// 所需数据选项
			typeList: [],
			options: [],
			optionsProps: {
				value: "id",
				label: "title",
				children: "children",
				emitPath: false,
				checkStrictly: true,
				disabled: (node) => {
					// 禁用本身
					return node.id === this.form.id;
				}
			},
			// 表单数据
			form: {
				id: null,
				title: null,
				parentId: null,
				type: "menu",
				icon: null,
				code: null,
				route: null,
				component: null,
				orderNum: 1,
				status: '1',
				hidden: false,
				affix: false,
				remark: null
			},
			// 表单验证规则
			rules: {
				title: [
					{required: true, message: '请输入标题', trigger: "blur"}
				],
				type: [
					{required: true, message: '请选择类型', trigger: 'blur'}
				],
				code: [
					{required: true, message: '请输入编码', trigger: 'blur'}
				],
				status: [
					{required: true, message: '请选择状态', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
		this.getOptions();
	},
	methods: {
		// 显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		// 表单注入数据
		setData(data) {
			this.form.id = data.id;
			this.form.title = data.title;
			this.form.parentId = data.parentId;
			this.form.type = data.type;
			this.form.icon = data.icon;
			this.form.code = data.code;
			this.form.route = data.route;
			this.form.component = data.component;
			this.form.orderNum = data.orderNum;
			this.form.status = data.status;
			this.form.hidden = data.hidden;
			this.form.affix = data.affix;
			this.form.remark = data.remark;
		},
		// 加载树数据
		async getOptions() {
			this.options = await this.$API.manage.permission.list.get();
			this.typeList = await this.$API.system.dict.getDataByCode.get({dictCode: 'menu_type'});
		},
		// 表单提交
		submit() {
			this.$refs.dialogForm.validate((valid) => {
				if (valid) {
					this.isSaving = true;
					if (this.form.id) {
						this.$API.manage.permission.edit.put(this.form).then(() => {
							this.$emit('success');
							this.visible = false;
							this.$message.success("保存成功");
						}).catch(() => {
							this.isSaving = false;
						});
					} else {
						this.$API.manage.permission.add.post(this.form).then(() => {
							this.$emit('success');
							this.visible = false;
							this.$message.success("保存成功");
						}).catch(() => {
							this.isSaving = false;
						});
					}
				}
			})
		}
	}
}
</script>

<style scoped>
h2 {
	font-size: 17px;
	color: #3c4a54;
	padding: 0 0 30px 0;
}

.apilist {
	border-left: 1px solid #eee;
}

[data-theme="dark"] h2 {
	color: #fff;
}

[data-theme="dark"] .apilist {
	border-color: #434343;
}
</style>
